
<template>
  <div class="wrap" style="padding: 15px">
    <el-row>
      <el-form :inline="true" :model="queryParams" class="demo-form-inline">
        <el-form-item label="礼品卡名称">
          <el-input
            clearable
            v-model="queryParams.name"
            placeholder="礼品卡名称"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <div class="block">
      <p>礼品卡总发放金额:{{ balance.sendAmount }}</p>
      <p>礼品卡总余额:{{ balance.balance }}</p>
      <p>总发放张数:{{ balance.sendNum }}</p>
      <p>已使用张数:{{ balance.usedNum }}</p>
    </div>
    <el-button type="primary" style="margin-bottom: 15px" @click="addGiftCard"
      >新增</el-button
    >

    <el-table :data="tableData" style="width: 100%" stripe v-loading="loading">
      <el-table-column label="礼品卡名称" prop="name"></el-table-column>
      <el-table-column label="充值金额" prop="amount"></el-table-column>
      <el-table-column label="已发放数量" prop="sendNum"></el-table-column>
      <el-table-column label="已发放金额" prop="sendAmount"></el-table-column>
      <el-table-column label="已使用数量" prop="usedNum"></el-table-column>
      <el-table-column label="已使用金额" prop="usedAmount"></el-table-column>
      <el-table-column label="状态" prop="status">
        <span slot-scope="scope">
          {{ scope.row.status == "0" ? "正常" : "停用" }}
        </span>
      </el-table-column>
      <el-table-column label="操作" width="400">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="lock(scope.row)"
            v-if="scope.row.status == 0"
            >停用</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click="unlock(scope.row)"
            v-if="scope.row.status == 1"
            >启用</el-button
          >
          <el-button type="danger" size="small" @click="del(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="text-align: right; justify-content: flex-end">
      <el-pagination
        @size-change="getData"
        @current-change="getData"
        :current-page.sync="queryParams.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size.sync="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <el-dialog title="会员充值" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" ref="myform">
        <el-form-item label="礼品卡名称" prop="amount">
          <el-input v-model="form.name" style="width: 244px"></el-input>
        </el-form-item>
        <el-form-item label="金额（元）" prop="amount">
          <el-input v-model="form.amount" style="width: 244px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { giftCardList, giftCardAdd, giftCardDel, giftCardDisable, memberServiceCardSummary } from '@/config/api'
import { times, divide } from "@/utils/tools"

export default {
  components: {
  },
  data () {
    return {
      queryParams: {
        name: undefined,
        page: 1,
        pageSize: 10,
      },
      tableData: [],
      levelList: [],
      total: 0,
      loading: false,
      balance: {},
      dialogFormVisible: false,
      form: {
        name: "",
        amount: ''
      }
    };
  },

  created () {
    this.getData()
    this.getBalance()

  },
  methods: {
    addGiftCard () {
      this.form = {
        name: "",
        amount: ''
      }
      this.dialogFormVisible = true
    },
    submit () {
      if (!this.form.name) {
        this.$confirm('请输入礼品卡名称?', '提示')
        return false
      }
      if (!this.form.amount) {
        this.$confirm('请输入礼品卡金额?', '提示')
        return false
      }
      if (this.form.amount < 1) {
        this.$confirm('场次价格不能小于1元?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        return false
      }
      let params = JSON.parse(JSON.stringify(this.form))
      params.amount = times(params.amount, 100)
      giftCardAdd(params).then(res => {
        this.dialogFormVisible = false
        this.getData()
      })
    },
    lock (record) {
      this.$confirm('此操作将进行停用, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        giftCardDisable({ id: record.id, status: 1 }).then(res => {
          this.getData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    unlock (record) {
      this.$confirm('此操作将解除停用, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        giftCardDisable({ id: record.id, status: 0 }).then(res => {
          this.getData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    del (record) {
      this.$confirm('此操作将删除礼品卡, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        giftCardDel({ id: record.id }).then(res => {
          this.getData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    getData () {
      giftCardList(this.queryParams).then(res => {
        res.data.result.list.map(item => {
          item.amount = divide(item.amount, 100)
          item.usedAmount = divide(item.usedAmount, 100)
          item.balance = divide(item.balance, 100)
          item.sendAmount = divide(item.sendAmount, 100)
        })
        this.tableData = res.data.result.list
        this.total = res.data.result.total
      })
    },

    getBalance () {
      memberServiceCardSummary({}).then(res => {
        res.data.result.balance = divide(res.data.result.balance || 0, 100)
        res.data.result.sendAmount = divide(res.data.result.sendAmount || 0, 100)
        this.balance = res.data.result
      })
    }
  },
};
</script>
<style lang="scss" scoped>
.block {
  display: flex;
  margin: 15px 0;
  p {
    font-size: 18px;
    font-weight: bold;
    margin-right: 50px;
  }
}
</style>